$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'eval/applyQuery',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', hidden: true, index: "id", width: 45, key: true },
            { label: '单号', name: 'sign', width: 75 },

            { label: '导员', name: 'gname', width: 75 },
            { label: '加分项', name: 'tname', width: 75 },
            { label: '佐证材料', name: 'img',width: 85,formatter:function(value,options,rowData){

                    if(value!=null && value.indexOf(",")>-1){
                        var imgs =value.split(",");
                        var igs="";
                        for (var i= 0;i<imgs.length;i++){
                          //  igs =igs+"<img  style='max-width: 50%; max-height: 50%' src='"+baseUrl+"/"+imgs[i]+"' /><br>";
                            igs =igs+  '<a class="view"  href="javascript:void(0)" >' +
                            '<img onclick="clickimg(\''+imgs[i]+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+imgs[i]+'" />' +
                            '</a>'
                        }
                        return igs;
                    }else if(value!=null && value.indexOf(",")==-1){
                        var s = '<a class="view"  href="javascript:void(0)" >' +
                            '<img onclick="clickimg(\''+value+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+value+'" />' +
                            '</a>';
                        return s;
                    }else{
                        return '';
                    }

                   }
                },
            { label: '申请时间', name: 'time',width: 85},
            { label: '操作',formatter:function(value,options,rowData){
                      return '<b><span  onclick="update('+rowData.id+')"  style="cursor:pointer;" class="label label-success">修改</span></b> | ' +
                          '<b><span  onclick="del('+rowData.id+')"  style="cursor:pointer;" class="label label-warning">删除</span></b>';

               }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,

        rownumbers: true,
        rownumWidth: 40,
        autowidth:true,

        jsonReader : {
            root: "list",

        },


        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

//点击之后放到图片
function clickimg(e){

    var url = baseUrl + '/' +e;

    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        area: ['50%', '50%'],
        skin: 'layui-layer-nobg', //没有背景色
        shadeClose: true,
        content: '<img style="width: 100%;height: 100%" src="'+url+'"/>'
    });
}


var zcvm = new Vue({
    el:'#zcapp',
    data:{
        atype: null,

        poptions: [],//一级下拉数组
        coptions: [],//二级下拉数组
        zcshowList: false,
        ap: {
            sid:null,
            tid:null,
            id:null,
            content:null,
            begin:null,
            end:null,
            img:null,
            btype:null
         }

    },
    created:function(){
        $.get(baseURL+"eval/typea",function (s) {
            zcvm.poptions = s;
        });
        // if(zcvm.ap.btype != null){
        //     $.get(baseURL+"eval/typeb",{"pid": zcvm.ap.btype},function (s) {
        //         zcvm.coptions = s;
        //     });
        // }


    },
    methods: {
        showapply: function(){
            zcvm.zcshowList = true;
            zcvm.atype = null;

            zcvm.ap = {
                id:null,
                sid:null,
                tid:null,
                content:null,
                begin:null,
                end:null,
                img:null,
                btype:null
            }
        },

        choose: function () {
            //获取被选中的一级下拉列表的值

            $.get(baseURL+"eval/typeb",{"pid": zcvm.atype},function (s) {
                zcvm.coptions = s;
            });

        },

        zcsave: function () {
            zcvm.ap.img =$("#photo").val();
            $.ajax({
                type: "POST",
                url: baseURL +  "eval/applyAdd",
                contentType: "application/json",
                data: JSON.stringify(zcvm.ap),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            zcvm.reload();

                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            zcvm.zcshowList = false;
            $("#jqGrid").trigger("reloadGrid");
        }

    }
});
function update(id) {

    zcvm.zcshowList = true;
    $.get(baseURL + "eval/edit/",{"id":id}, function(r){
        zcvm.atype = r.apply.atype;
        zcvm.coptions =r.list;

        zcvm.ap = r.apply;
        console.log(zcvm.ap);
    });
}

function del(id) {


    $.get(baseURL + "eval/del/",{"id":id}, function(r){

        zcvm.reload();
    });
}
var baseUrl= baseUrl();
function baseUrl()
{
    //获取当前网址，如： http://localhost:8080/myproj/view/my.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： myprojiew/my.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/myproj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    //得到了 http://localhost:8080/myproj
    var realPath=localhostPaht+projectName;
    return realPath;
    //alert(realPath);
}